﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.Xml;

namespace Police.Configuration
{
    /// <summary>
    /// Contains general configuration for Police.
    /// </summary>
    [Serializable]
    public class Settings
    {
        /// <summary>
        /// Gets or sets list of developers with personal informations.
        /// </summary>
        [XmlArray(ElementName="Developers")]
        public List<Developer> Developers { get; set; }

        /// <summary>
        /// Gets or sets list of solutions managed by Police.
        /// </summary>
        [XmlArray(ElementName = "Solutions")]
        public List<Solution> Solutions { get; set; }

        /// <summary>
        /// Save current configuration in specified file path.
        /// </summary>
        /// <param name="filename">Complete file path where configuration will be saved.</param>
        public void Save(String filename)
        {
            XmlSerializer serializer = new XmlSerializer(GetType());
            using (XmlWriter writer = XmlWriter.Create(filename))
            {
                serializer.Serialize(writer, this);
                writer.Flush();
                // writer.Close();
            }
            serializer = null;
        }

        /// <summary>
        /// Load configuration from specified file.
        /// </summary>
        /// <param name="filename">Complete path where file is stored.</param>
        /// <returns>Returns settings loaded from file.</returns>
        public static Settings Load(String filename)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(Settings));
            using (XmlReader reader = XmlReader.Create(filename))
            {
                Settings settings = (Settings)serializer.Deserialize(reader);
                // reader.Close();
                return settings;
            }
        }
    }
}
